package com.example.evaluate.controller;

import com.example.evaluate.model.Question;
import com.example.evaluate.model.TeacherCourse;
import com.example.evaluate.model.TrueTeacher;
import com.example.evaluate.model.temp.StudentInfo;
import com.example.evaluate.model.temp.TeacherInfo;
import com.example.evaluate.service.StudentService;
import com.example.evaluate.service.TeacherService;
import com.example.evaluate.service.TempService;
import com.example.evaluate.util.CalculateUtil;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
public class GetExcelController {

    @Autowired
    StudentService studentService;

    @Autowired
    TeacherService teacherService;

    @Autowired
    TempService tempService;

    /**
     * 根据老师的课程导出对老师的评价
     * */
    @RequestMapping("/getQuestionAdviceByTeacherCourse")
    public void getAdministrationExcel(HttpServletResponse response, HttpServletRequest request,String courseName) throws IOException {
        System.out.println("courseName:"+courseName);
        response.setCharacterEncoding("UTF-8");

        //创建excel文件
        HSSFWorkbook wb = new HSSFWorkbook();
        //创建sheet页
        HSSFSheet sheet = wb.createSheet("老师评价表");
        //创建标题行
        HSSFRow titleRow = sheet.createRow(0);
        titleRow.createCell(0).setCellValue("总分");
        titleRow.createCell(1).setCellValue("题目1");
        titleRow.createCell(2).setCellValue("题目2");
        titleRow.createCell(3).setCellValue("题目3");
        titleRow.createCell(4).setCellValue("题目4");
        titleRow.createCell(5).setCellValue("题目5");
        titleRow.createCell(6).setCellValue("题目6");
        titleRow.createCell(7).setCellValue("题目7");
        titleRow.createCell(8).setCellValue("题目8");
        titleRow.createCell(9).setCellValue("题目9");
        titleRow.createCell(10).setCellValue("题目10");
        titleRow.createCell(11).setCellValue("对老师的建议");
        //通过专业获取问题

        List<Question> questions = teacherService.findQuestionByCourseName(courseName);

        //遍历将数据放到excel列中
        for (Question q : questions) {
            HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum() + 1);
            dataRow.createCell(0).setCellValue(CalculateUtil.getTotalPoint(q));
            dataRow.createCell(1).setCellValue(CalculateUtil.getQuestionString(q.getQuestion1()));
            dataRow.createCell(2).setCellValue(CalculateUtil.getQuestionString(q.getQuestion2()));
            dataRow.createCell(3).setCellValue(CalculateUtil.getQuestionString(q.getQuestion3()));
            dataRow.createCell(4).setCellValue(CalculateUtil.getQuestionString(q.getQuestion4()));
            dataRow.createCell(5).setCellValue(CalculateUtil.getQuestionString(q.getQuestion5()));
            dataRow.createCell(6).setCellValue(CalculateUtil.getQuestionString(q.getQuestion6()));
            dataRow.createCell(7).setCellValue(CalculateUtil.getQuestionString(q.getQuestion7()));
            dataRow.createCell(8).setCellValue(CalculateUtil.getQuestionString(q.getQuestion8()));
            dataRow.createCell(9).setCellValue(CalculateUtil.getQuestionString(q.getQuestion9()));
            dataRow.createCell(10).setCellValue(CalculateUtil.getQuestionString(q.getQuestion10()));
            dataRow.createCell(11).setCellValue(q.getQuestionAdvice());
        }
        setExcelDownload(response,wb);
    }


    /**
     * 导出所有老师的信息
     * */
    @RequestMapping("/getAllTeacherInfoExcel")
    public void getAllTeacherInfoExcel(HttpServletResponse response, HttpServletRequest request) throws IOException {

        response.setCharacterEncoding("UTF-8");

        //创建excel文件
        HSSFWorkbook wb = new HSSFWorkbook();
        //创建sheet页
        HSSFSheet sheet = wb.createSheet("老师信息");
        //创建标题行
        HSSFRow titleRow = sheet.createRow(0);
        titleRow.createCell(0).setCellValue("老师工号");
        titleRow.createCell(1).setCellValue("老师姓名");
        titleRow.createCell(2).setCellValue("老师密码");
        titleRow.createCell(3).setCellValue("课程1");
        titleRow.createCell(4).setCellValue("课程2");
        titleRow.createCell(5).setCellValue("课程3");


        List<TeacherInfo> teacherInfos =tempService.getAllTeacherInfo();
        //遍历将数据放到excel列中
        for (TeacherInfo t : teacherInfos) {
            HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum() + 1);
            dataRow.createCell(0).setCellValue(t.getTrueTeacher().getTrueTeacherId());
            dataRow.createCell(1).setCellValue(t.getTrueTeacher().getTrueTeacherName());
            dataRow.createCell(2).setCellValue(t.getTrueTeacher().getTrueTeacherPassword());

            for (int i = 0; i < t.getTeacherCourses().size(); i++) {

                dataRow.createCell(i+3).setCellValue(t.getTeacherCourses().get(i).getCourseName());
            }
        }
        setExcelDownload(response,wb);
    }


    /**
     * 导出所有学生的信息
     * */
    @RequestMapping("/getAllStudentInfoExcel")
    public void getAllStudentInfoExcel(HttpServletResponse response, HttpServletRequest request) throws IOException {

        response.setCharacterEncoding("UTF-8");

        //创建excel文件
        HSSFWorkbook wb = new HSSFWorkbook();
        //创建sheet页
        HSSFSheet sheet = wb.createSheet("学生信息");
        //创建标题行
        HSSFRow titleRow = sheet.createRow(0);
        titleRow.createCell(0).setCellValue("学生学号");
        titleRow.createCell(1).setCellValue("学生姓名");
        titleRow.createCell(2).setCellValue("学生学院");
        titleRow.createCell(3).setCellValue("学生专业");
        titleRow.createCell(4).setCellValue("学生班级");
        titleRow.createCell(5).setCellValue("课程1");
        titleRow.createCell(6).setCellValue("课程2");
        titleRow.createCell(7).setCellValue("课程3");
        titleRow.createCell(8).setCellValue("课程4");
        titleRow.createCell(9).setCellValue("课程5");
        //
        List<StudentInfo> studentInfos =tempService.getAllStudentInfo();
        //遍历将数据放到excel列中
        for (StudentInfo s : studentInfos) {
            HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum() + 1);
            dataRow.createCell(0).setCellValue(s.getStudentId());
            dataRow.createCell(1).setCellValue(s.getStudentName());
            dataRow.createCell(2).setCellValue(s.getStudentAcademy());
            dataRow.createCell(3).setCellValue(s.getStudentMajor());
            dataRow.createCell(4).setCellValue(s.getStudentClass());

            for (int i = 0; i < s.getTeachers().size(); i++) {

                dataRow.createCell(i+5).setCellValue( s.getTeachers().get(i).getTeacherCourse());
            }
        }
        setExcelDownload(response,wb);
    }


    /**
     * 设置下载客户端的名称
     * */
    private void setExcelDownload(HttpServletResponse response,HSSFWorkbook wb) throws IOException {
        //设置下载时客户端Excel的名称
        String filename = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + ".xls";
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-disposition", "attachment;filename=" + filename);
        OutputStream outputStream = response.getOutputStream();
        wb.write(outputStream);
        outputStream.flush();
        outputStream.close();
    }



}
